package com.fwj.multiformat;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.Iterator;
import java.util.List;

/**
 * @author 傅文江
 * <p>
 * 时间： 2018-03-30 15:32
 * <p>
 * 描述：
 * <p>
 * 版权： Copyright © 2018
 **/
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    @Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }

//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        converters.add(customJackson2HttpMessageConverter());
//        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();
//
//        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));
//    }

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
//        while (iterator.hasNext()) {
//            HttpMessageConverter converter = iterator.next();
//            if(converter instanceof org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter
//                    || converter instanceof org.springframework.http.converter.json.MappingJackson2HttpMessageConverter){
//                converters.remove(converter);
//            }
//        }
    }
}